const express = require('express');
const mysql = require('mysql');
const sign = require('../../libs/common-util');
const config = require('../../config');

const db = mysql.createPool({
    host : config.databaseHost,//,'localhost',
    user : config.databaseUser,//'root',
    password : config.databasePwd,//'Deng%)#@real5032',
    database : config.database,//'learner'
});

module.exports = function() {
    var adminRouter = express.Router();

    //检测登录状态
    //
    adminRouter.use((req, res, next) => {//拦截admin下面的所有请求，检测用户是否合法
        if (!req.session['admin_id'] && req.url != '/login') { //如果sessionId为空，以及重定向不为login动作
            res.redirect('/admin/login');
        }else {
            next();
        }
    });

    adminRouter.get('/', (req, res)=> {
        res.render('admin/index.ejs');
    });

    adminRouter.use('/login', require('./login')());
    adminRouter.use('/banners', require('./banners')());
    return adminRouter;
}